package capstoneproject.dudiachi.Main;

import java.util.ArrayList;

import capstoneproject.dudiachi.Model.NavDrawerItem;

import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.SearchView;

public class BaseActivity extends FragmentActivity {

	public DrawerLayout drawerLayout;
	public ListView drawerList;
	public String[] menu_array;
	private ActionBarDrawerToggle drawerToggle;
	private DrawerLayout fullLayout;
	private String[] navDrawerTitles;
	private TypedArray navDrawerIcons;
	private ArrayList<NavDrawerItem> navDrawerItems;
	private NavDrawerListAdapter navAdapter;
	public static int POS;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void setContentView(int layoutResID) {

		fullLayout = (DrawerLayout) getLayoutInflater().inflate(
				R.layout.nav_drawer, null);
		FrameLayout actContent = (FrameLayout) fullLayout
				.findViewById(R.id.content_frame);

		navDrawerTitles = getResources().getStringArray(R.array.nav_drawer_items);
		navDrawerIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
		navDrawerItems = new ArrayList<NavDrawerItem>();
		
		drawerLayout = (DrawerLayout) fullLayout.findViewById(R.id.drawer_layout);
		drawerList = (ListView) fullLayout.findViewById(R.id.left_drawer);
		
		navDrawerItems.add(new NavDrawerItem(navDrawerTitles[0], navDrawerIcons.getResourceId(0, -1)));
		navDrawerItems.add(new NavDrawerItem(navDrawerTitles[1], navDrawerIcons.getResourceId(1, -1)));
		navDrawerItems.add(new NavDrawerItem(navDrawerTitles[2], navDrawerIcons.getResourceId(2, -1)));
		navDrawerItems.add(new NavDrawerItem(navDrawerTitles[3], navDrawerIcons.getResourceId(3, -1)));
		navDrawerItems.add(new NavDrawerItem(navDrawerTitles[4], navDrawerIcons.getResourceId(4, -1)));
		navDrawerItems.add(new NavDrawerItem(navDrawerTitles[5], navDrawerIcons.getResourceId(5, -1)));
		navDrawerIcons.recycle();
		
		navAdapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
		drawerList.setAdapter(navAdapter);
		
		drawerList.setOnItemClickListener(new DrawerItemClickListener());
		drawerList.setItemChecked(POS, true);
		drawerList.setSelection(POS);
		
		
		drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout,
				R.drawable.ic_drawer, 0, 0) {
			public void onDrawerClosed(View view) {
				getActionBar().setTitle(getTitle());
				invalidateOptionsMenu();
			}

			public void onDrawerOpened(View drawerView) {
				getActionBar().setTitle(R.string.title_activity_left_menu);
				invalidateOptionsMenu();
			}
		};
		drawerLayout.setDrawerListener(drawerToggle);

		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setHomeButtonEnabled(true);
		
		getLayoutInflater().inflate(layoutResID, actContent, true);
		super.setContentView(fullLayout);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);

		SearchManager sm = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
		SearchView sv = (SearchView) menu.findItem(R.id.action_search).getActionView();
		sv.setSearchableInfo(sm.getSearchableInfo(getComponentName()));
		
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (drawerToggle.onOptionsItemSelected(item)) {
			return true;
		}
		switch (item.getItemId()) {
		case R.id.action_search:
			return true;
			
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private class DrawerItemClickListener implements
			ListView.OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			selectItem(position);
			
		}
	}

	private void selectItem(int position) {
		SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
		switch (position) {
		case 0:
			
				Intent diachi = new Intent(getApplicationContext(),
						OldProvinceActivity.class);
				startActivity(diachi);
				finish();
			
			break;

		case 1:
			
				
				String FestivalAccessMethod = sharedPref.getString(SettingFragment.TRUY_CAP_LE_HOI, "");
				if (FestivalAccessMethod.equals("1")) {
					Intent lehoi = new Intent(getApplicationContext(),
							FestivalCalendarActivity.class);
					startActivity(lehoi);
					finish();
				}
				if (FestivalAccessMethod.equals("2")) {
					Intent lehoi = new Intent(getApplicationContext(),
							FestivalProvinceActivity.class);
					startActivity(lehoi);
					finish();
				}
			
			break;
		case 2:
			
				
				String PlaceAccessMethod = sharedPref.getString(SettingFragment.TRUY_CAP_DIA_DANH, "");
				if (PlaceAccessMethod.equals("1")) {
					Intent diadanh = new Intent(getApplicationContext(),
							GoogleMapActivity.class);
					startActivity(diadanh);
					finish();
				}
				if (PlaceAccessMethod.equals("2")) {
					Intent diadanh = new Intent(getApplicationContext(),
							ProvinceActivity.class);
					startActivity(diadanh);
					finish();
				}
			
			break;
		case 3:
				Intent biendao = new Intent(getApplicationContext(),
						IslandArticleTypeActivity.class);
				startActivity(biendao);
				finish();
			
			break;
		case 4:
			if (POS != 4) {
				Intent setting = new Intent(getApplicationContext(),
						SettingActivity.class);
				startActivity(setting);
				finish();
			}
			break;
		case 5:
			if (POS != 5) {
				Intent home = new Intent(getApplicationContext(),
						HomeActivity.class);
				startActivity(home);
				finish();
			}
			break;
		default:
			break;
		}

	}

	public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = drawerLayout.isDrawerOpen(drawerList);
        menu.findItem(R.id.action_search).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }
	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		drawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		drawerToggle.onConfigurationChanged(newConfig);
	}
}
